<?php
declare(strict_types=1);
namespace App\Http\Controller;

use App\Model\Dao\Specific\IServerUser;
use Psr\Http\Message\RequestInterface;
use Swoft\Bean\Annotation\Mapping\Inject;
use Swoft\Http\Message\Contract\ServerRequestInterface;
use Swoft\Http\Message\Request;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Swoft\Http\Server\Annotation\Mapping\RequestMethod;
use Validate\CheckParam;
use Vadlidate\ValidateUserLogin;
use Tool\AjaxReturn;
/**
 * Class LoginController
 * @Controller(prefix="login")
 * @package App\Http\Controller
 */
class LoginController
{
    /**
     * @Inject()
     * @var IServerUser
     */
    private IServerUser $iserverUserData;

    /**
     * @RequestMapping(name="handle", method={RequestMethod::POST})
     * @param RequestInterface $request
     * @return array
     */
    public function handle(Request $request): array
    {
        $data = $request->post();
        
        $check = new CheckParam(ValidateUserLogin::getRule(), $data);
        
        if (!$check->detectionParameters()) {
            return AjaxReturn::error($check->getErrorMessage());
        }
      
        $errorString = '';
        
        $token = $this->iserverUserData->parseUser($data, $errorString);
       
        if ('' === $token) {
            return AjaxReturn::error($errorString);
        }
        
        return AjaxReturn::sendData($token);
    }
    
    /**
     * @RequestMapping(name="login", method={RequestMethod::POST})
     * @param Request $request
     * @return array
     */
    public function login(Request $request): array {
        
        if (!$this->iserverUserData->getRedis()->get($request->post("token"))) {
            return AjaxReturn::error('请登录');
        }
        
        return AjaxReturn::sendData('');;
    }
    
  
}